import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import { Trip } from './trip';

import {ActivatedRoute, Params, Router} from '@angular/router';
import { TripService } from './trip.service';

import { CarrierService } from '../carrier/carrier.service';
import { Carrier } from '../carrier/carrier';

import { DriverService } from '../driver/driver.service';
import { Driver } from '../driver/driver';

import { BusService } from '../bus/bus.service';
import { Bus } from '../bus/bus';
import {TripComponent} from "./trip.component";

@Component({
    selector: 'trip-detail',
    templateUrl: './trip-detail.component.html',
    // styleUrls: ['./trip-detail.component.css']
})

export class TripDetailComponent implements OnInit {
    @Input()
    trip: Trip;
    @Output() onVoted = new EventEmitter<boolean>();
    buses: Bus[];
    drivers: Driver[];
    carriers: Carrier[];
    constructor(
        private tripService: TripService,
        private carrierService: CarrierService,
        private busService: BusService,
        private driverService: DriverService,
        private router: ActivatedRoute) {

    }

    ngOnInit() {
        this.router.params.forEach((params: Params) => {
            let trip_id = +params['trip_id'];
            this.tripService.getTrip(trip_id).then(trip => this.trip =trip);

            this.carrierService.getCarriers().then(carriers => this.carriers = carriers);
            this.busService.getBuss().then(buses => this.buses = buses);
            this.driverService.getDrivers().then(drivers => this.drivers = drivers);
        });
    }

    behind(data: Trip){
      console.log('--------');
      this.trip = data;
     // console.log('、、、、、'+data.carrier_id);
      /*
      this.busService.getBussByCarrierID(this.trip.carrier_id).then(buses => this.buses = buses);
      this.driverService.getDriversByCarrierID(this.trip.carrier_id).then(drivers => this.drivers = drivers);
      */
    }


    goBack() {
        window.history.back();
    }

    save(): void {
        this.tripService.save(this.trip).then(() => this.cancel());
    }


    delete(): void {
        this.tripService.delete(this.trip).then(() => this.cancel());
    }

    cancel(): void {
        this.trip = null;
        this.onVoted.emit();
    }

    /*refresh(carrier_id: number): void{
        this.busService.getBussByCarrierID(carrier_id+'').then(buses => this.buses = buses);
        this.driverService.getDriversByCarrierID(carrier_id+'').then(drivers => this.drivers = drivers);
    }*/
}
